package com.youxuit.youxu.kecheng.threadPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest1 {
    public static void main(String[] args) throws InterruptedException{

        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);

        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();

        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(10);

        long start = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            cachedThreadPool.submit(new MyTask(i));
        }
        cachedThreadPool.shutdown();
        boolean isStop = cachedThreadPool.awaitTermination(1, TimeUnit.HOURS);
        System.out.println("is pool finished: " + isStop);
        System.out.println("threadPollTest耗时：" + (System.currentTimeMillis() - start));
    }
}

class MyTask implements Runnable {

    int i = 0;

    public MyTask(int i) {
        this.i = i;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "程序员做第" + i + "个项目");
        try {
            Thread.sleep(3000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
